activiti和设计模式(6)

流程实力启动以后,任务的完成是一个比较重要的点,到底怎样才算完成了该节点对应的任务了,可以向下一节点推进了呢?

state Pattern:

The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern’s superclass.

The state pattern can be interpreted as a strategy pattern which is able to switch the current strategy through invocations of methods defined in the pattern’s interface.

状态模型和策略模型可以相互的转化

This pattern is used in computer programming to encapsulate varying behavior for the same object based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements[1]:395 and thus improve maintainability.[2]

启动的开始:

processInstance.start();
//这里的ProcessInstance的类型是ExecutionEntity,不管是流程定义对应的流程实例,还是活动定义对应的活动实例,都是ExecutionEntity,仔细查看会发现,流程实例的ExecutionEntity和活动实例非常的相似

//流程实例启动
public void start() {
    if(startingExecution == null && isProcessInstanceType()) {
      startingExecution = new StartingExecution(processDefinition.getInitial());
    }
    performOperation(AtomicOperation.PROCESS_START);
  }

  //活动实例启动
  performOperation(AtomicOperation.ACTIVITY_START);

  //最终都是
  public void performOperation(AtomicOperation executionOperation) {
    if (executionOperation.isAsync(this)) {
      scheduleAtomicOperationAsync(executionOperation);
    } else {
      performOperationSync(executionOperation);
    }    
  }

  protected void performOperationSync(AtomicOperation executionOperation) {
    Context
      .getCommandContext()
      .performOperation(executionOperation, this);
  }

AtomicOperation相当于定义了流程流转的动作,所有的流程的动作都在这个接口中定义

public interface AtomicOperation {
  AtomicOperation PROCESS_START = new AtomicOperationProcessStart();
  AtomicOperation PROCESS_START_INITIAL = new AtomicOperationProcessStartInitial();
  AtomicOperation PROCESS_END = new AtomicOperationProcessEnd();
  AtomicOperation ACTIVITY_START = new AtomicOperationActivityStart();
  AtomicOperation ACTIVITY_EXECUTE = new AtomicOperationActivityExecute();
  AtomicOperation ACTIVITY_END = new AtomicOperationActivityEnd();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_END = new AtomicOperationTransitionNotifyListenerEnd();
  AtomicOperation TRANSITION_DESTROY_SCOPE = new AtomicOperationTransitionDestroyScope();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_TAKE = new AtomicOperationTransitionNotifyListenerTake();
  AtomicOperation TRANSITION_CREATE_SCOPE = new AtomicOperationTransitionCreateScope();
  AtomicOperation TRANSITION_NOTIFY_LISTENER_START = new AtomicOperationTransitionNotifyListenerStart();
  AtomicOperation DELETE_CASCADE = new AtomicOperationDeleteCascade();
  AtomicOperation DELETE_CASCADE_FIRE_ACTIVITY_END = new AtomicOperationDeleteCascadeFireActivityEnd();
  void execute(InterpretableExecution execution);
  boolean isAsync(InterpretableExecution execution);
}

具体动作的跳转见下图:

流程运转

就拿流程实例启动来说:AtomicOperationProcessStart来说,首先是抽象类里面的, void execute(InterpretableExecution execution);

 public void execute(InterpretableExecution execution) {
 //首先是判断监听的逻辑
    ScopeImpl scope = getScope(execution);
    List<ExecutionListener> exectionListeners = scope.getExecutionListeners(getEventName());
    int executionListenerIndex = execution.getExecutionListenerIndex();
    if (exectionListeners.size()>executionListenerIndex) {
      execution.setEventName(getEventName());
      execution.setEventSource(scope);
      ExecutionListener listener = exectionListeners.get(executionListenerIndex);
      try {
        listener.notify(execution);
      } catch (RuntimeException e) {
        throw e;
      } catch (Exception e) {
        throw new PvmException("couldn't execute event listener : "+e.getMessage(), e);
      }
      execution.setExecutionListenerIndex(executionListenerIndex+1);
      execution.performOperation(this);

    } else {
      execution.setExecutionListenerIndex(0);
      execution.setEventName(null);
      execution.setEventSource(null);
      eventNotificationsCompleted(execution);
    }
  }

然后就是自己实现的eventNotificationsCompleted(execution);

 @Override
  protected void eventNotificationsCompleted(InterpretableExecution execution) {
  //首先还是事件通知
  	if (Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
  	  Map<String, Object> variablesMap = null;
  	  try {
  	    variablesMap = execution.getVariables();
  	  } catch (Throwable t) {
  	    // In some rare cases getting the execution variables can fail (JPA entity load failure for example)
  	    // We ignore the exception here, because it's only meant to include variables in the initialized event.
  	  }
    	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
    			ActivitiEventBuilder.createEntityWithVariablesEvent(ActivitiEventType.ENTITY_INITIALIZED,
    			    execution, variablesMap, false));
      Context.getProcessEngineConfiguration().getEventDispatcher()
              .dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(execution, variablesMap, false));
    }
  	//找到开始节点,直接的跳转到PROCESS_START_INITIAL
    ProcessDefinitionImpl processDefinition = execution.getProcessDefinition();
    StartingExecution startingExecution = execution.getStartingExecution();
    List<ActivityImpl> initialActivityStack = processDefinition.getInitialActivityStack(startingExecution.getInitial());  
    execution.setActivity(initialActivityStack.get(0));
    execution.performOperation(PROCESS_START_INITIAL);//说明①
  }

说明①:流程实例启动后,直接的跳转到:PROCESS_START_INITIAL,即:

AtomicOperation PROCESS_START_INITIAL = new AtomicOperationProcessStartInitial();

执行的过程是,首先是: org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(InterpretableExecution)

然后才是正式的逻辑:

  @Override
  protected void eventNotificationsCompleted(InterpretableExecution execution) {
    ActivityImpl activity = (ActivityImpl) execution.getActivity();

    ProcessDefinitionImpl processDefinition = execution.getProcessDefinition();

    StartingExecution startingExecution = execution.getStartingExecution();
    //如果开始节点是起始节点
    if (activity==startingExecution.getInitial()) {
      execution.disposeStartingExecution();
      //转移到ACTIVITY_EXECUTE,执行activity对应的逻辑
      execution.performOperation(ACTIVITY_EXECUTE);

    } else {
    //如果不是开始节点,循环的找到开始节点
      List<ActivityImpl> initialActivityStack = processDefinition.getInitialActivityStack(startingExecution.getInitial());
      int index = initialActivityStack.indexOf(activity);
      activity = initialActivityStack.get(index+1);

      InterpretableExecution executionToUse = null;
      if (activity.isScope()) {
        executionToUse = (InterpretableExecution) execution.getExecutions().get(0);
      } else {
        executionToUse = execution;
      }
      executionToUse.setActivity(activity);
      executionToUse.performOperation(PROCESS_START_INITIAL);
    }
  }

然后就到了activity的执行的逻辑:

AtomicOperation ACTIVITY_EXECUTE = new AtomicOperationActivityExecute();

具体的执行的逻辑是:

public void execute(InterpretableExecution execution) {
    ActivityImpl activity = (ActivityImpl) execution.getActivity();

    ActivityBehavior activityBehavior = activity.getActivityBehavior();
    if (activityBehavior==null) {
      throw new PvmException("no behavior specified in "+activity);
    }

    log.debug("{} executes {}: {}", execution, activity, activityBehavior.getClass().getName());

    try {
    	if(Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
      	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
      			ActivitiEventBuilder.createActivityEvent(ActivitiEventType.ACTIVITY_STARTED,
      					execution.getActivity().getId(),
      					(String) execution.getActivity().getProperty("name"),
      					execution.getId(),
      					execution.getProcessInstanceId(),
      					execution.getProcessDefinitionId(),
      					(String) activity.getProperties().get("type"),
      					activity.getActivityBehavior().getClass().getCanonicalName()));
      }

      activityBehavior.execute(execution);
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      LogMDC.putMDCExecution(execution);
      throw new PvmException("couldn't execute activity <"+activity.getProperty("type")+" id=\""+activity.getId()+"\" ...>: "+e.getMessage(), e);
    }
  }

activity的行为活动就非常的多了:

public interface ActivityBehavior extends Serializable {
  void execute(ActivityExecution execution) throws Exception;
}

比较熟悉activitybehavior: 熟悉的activitybehavior

Powered by andiHappy and Theme by AndiHappy